package com.liu.sx.service;

import com.liu.sx.common.util.IdWorker;
import com.liu.sx.dao.DriverDao;
import com.liu.sx.entity.Driver;
import com.liu.sx.pojo.BW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Service
@Transactional
public class DriverService {

    @Autowired
    private DriverDao driverDao;

    @Autowired
    private IdWorker idWorker;


    public void  insert(BW bw){
        Driver driver =driverDao.findByAddress(bw.getAddress());
        if(driver==null){
            driver = new Driver();
            driver.setAddress(bw.getAddress());

        }
    }

    public void addDriver(Driver driver){
        driver.setId(idWorker.nextId()+"");
        driverDao.save(driver);
    }

    public Page<Driver> findAll(int num, int rows, String query) {
        Pageable pageable = PageRequest.of(num-1,rows, Sort.Direction.DESC,"createTime");

        return  driverDao.findAll(query,pageable);



        }


        public List<Driver> findByUserId(int userId){
         return driverDao.findByUserId(userId);
        }


}
